﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Extension;
using Common.Model;
using HotTopic.collector;
using HotTopic.service;
using log4net;

namespace HotTopic.Script
{
    public class InitScript
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(InitScript));
        
        public static readonly string[] DropList= {
            "奢侈品",
            "珠三角",
            "通达信88",
            "成渝特区",
            "重组概念",
            "长三角",
            "融资融券",
            "环渤海",
            "农村金融",
            "含H股",
            "H股",
            "大盘",
            "二马",
            "基金重仓",
            "增持回购",
            "预盈预增",
            "员工持股",
            "沪警示板",
            "S板",
            "中证",
            "百元股",
            "低价",
            "社保重仓",
            "QFII持股",
            "小盘",
            "长株潭",
            "皖江区域",
            "黄河三角",
            "含B股",
            "聚氨酯",
            "草甘膦",
            "新三板",
            "含可转债",
            "图们江",
            "ST板块",
            "蓝宝石",
            "宽带提速",
            "保障房",
            "电商概念",
            "武汉规划",
            "维生素"
        };

        public static void Run()
        {
            Log.Info("init script start");
            
            var dzhCollector = new DzhCollector();
            var thsCollector = new ThsCollector();
            var tdxCollector = new TdxCollector();

            Dictionary<String, List<String>> dzhTopicsDic = dzhCollector.GetTopics();
            Dictionary<String, List<String>> thsTopicsDic = thsCollector.GetTopics();
            Dictionary<String, List<String>> tdxTopicsDic = tdxCollector.GetTopics();

            var dzhHisTopics = new Dictionary<string, Topic>();
            var thsHisTopics = new Dictionary<string, Topic>();
            var tdxHisTopics = new Dictionary<string, Topic>();

            foreach (var topicName in dzhTopicsDic.Keys)
            {
                var set = from code in dzhTopicsDic.Get(topicName)
                          where true
                          select new Stock { StockCode = code };

                var topic = new Topic
                {
                    TopicName = topicName,
                    From = Constant.dzhname,
                    Stocks = new HashSet<Stock>(set)
                };
                dzhHisTopics.Add(topicName, topic);
            }

            foreach (var topicName in thsTopicsDic.Keys)
            {
                
                var set = from code in thsTopicsDic.Get(topicName)
                          where true
                          select new Stock { StockCode = code };

                var topic = new Topic
                {
                    TopicName = topicName,
                    From = Constant.thsname,
                    Stocks = new HashSet<Stock>(set)
                };
                thsHisTopics.Add(topicName, topic);
            }

            foreach (var topicName in tdxTopicsDic.Keys)
            {
                var set = from code in tdxTopicsDic.Get(topicName)
                          where true
                          select new Stock { StockCode = code };

                var topic = new Topic
                {
                    TopicName = topicName,
                    From = Constant.tdxname,
                    Stocks = new HashSet<Stock>(set)
                };
                tdxHisTopics.Add(topicName, topic);
            }

            TopicNameRedisDbProvider.CacheHisTopics(Constant.thsname, thsHisTopics);
            TopicNameRedisDbProvider.CacheHisTopics(Constant.tdxname, tdxHisTopics);
            TopicNameRedisDbProvider.CacheHisTopics(Constant.dzhname, dzhHisTopics);

            var enumerable = from topicName in DropList
                             where tdxHisTopics.ContainsKey(topicName)
                             select tdxHisTopics.Get(topicName);

            var dictionary = enumerable.ToDictionary(topic => topic.TopicName, topic => topic);
            TopicNameRedisDbProvider.CacheTopicsDrop(dictionary);
            TopicNameRedisDbProvider.CacheMainTopic(new Dictionary<string, Topic>());
            Log.Info("init script end");
        }
    }
}